package com.thinkgem.jeesite.modules.bu.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.bu.entity.BuOrder;
import com.thinkgem.jeesite.modules.bu.entity.BuOrderCp;
import com.thinkgem.jeesite.modules.bu.entity.BuSalesman;
import com.thinkgem.jeesite.modules.bu.service.BuOrderCpService;
import com.thinkgem.jeesite.modules.bu.service.BuOrderService;
import com.thinkgem.jeesite.modules.bu.service.BuSalesmanService;
import com.thinkgem.jeesite.modules.bu.service.IBuCustomerService;
import com.thinkgem.jeesite.modules.bu.service.IBuOrderService;
import com.thinkgem.jeesite.modules.bu.service.IBuSupplierService;
import com.thinkgem.jeesite.modules.bu.utils.DateUtil;
import com.thinkgem.jeesite.modules.bu.utils.ExcelTest;
import com.thinkgem.jeesite.modules.bu.utils.OrderStateEnum;
import com.thinkgem.jeesite.modules.cms.entity.Site;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.IUserService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 外部订单Controller
 * 
 * @author
 * @version 2018-08-26
 */
@Controller
@RequestMapping(value = "${adminPath}/bu/buExternalOrder")
public class BuExternalOrderController extends BaseController {

	@Autowired
	private BuOrderService buOrderService;

	@Autowired
	private IBuOrderService iBuOrderService;

	@Resource
	private IBuSupplierService buSupplierService;

	@Resource
	private IBuCustomerService buCustomerService;

	@Resource
	private BuOrderCpService buOrderCpService;

	@Resource
	private IUserService userService;

	@Resource
	private BuSalesmanService buSalesManService;
	
	@Value("${httpPriffix}")
	private String httpPriffix;

	@Value("${win.userfiles.basedir}")
	private String winUserfilesBasedir;
	
	@Value("${linux.userfiles.basedir}")
	private String linuxUserfilesBasedir;
	
	@ModelAttribute
	public BuOrder get(@RequestParam(required = false) String id) {
		BuOrder entity = null;
		if (StringUtils.isNotBlank(id)) {
			BuOrder order = new BuOrder();
			order.setId(id);
			order.setDelFlag("0");
			entity = buOrderService.get(order);
		}
		if (entity == null) {
			entity = new BuOrder();
		}
		return entity;
	}

	@RequiresPermissions("bu:buExternalOrder:view")
	@RequestMapping(value = { "list", "" })
	public String list(BuOrder buOrder, HttpServletRequest request, HttpServletResponse response, Model model) {

		// 订单状态(0:待审核;1:待生产;2:生产中;3:待发货;4:待收货;5:待收款;6:已完成)
		User user = UserUtils.getUser();
		List<String> roleList = user.getRoleIdList();
		Page<BuOrder> page = null;
		String roleId = String.valueOf(roleList.get(0));
		List<String> orderStateList = new ArrayList<>();
		String yeCode = "6";
		String csCode = "7952384aba3e401aa0b4cc713d5b71b1";
		String gdCode = "1a894ce8e6014efb9d21fb922bf8ceb2";
		
		String csCode_001 = "fc7990c5e8b3476a900adf27eff22124";
		
		if (roleId.equals(yeCode)) { // 业务员角色ID
			orderStateList.add(String.valueOf(OrderStateEnum.C1000.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1001.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1002.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1003.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1004.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1005.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1006.getCode()));
			buOrder.setCustomerId(user.getId());
			buOrder.setOrderStateList(orderStateList);
			buOrder.setOrderType("1");
			page = iBuOrderService.findCustomerPage(new Page<BuOrder>(request, response), buOrder);
		} else if (roleId.equals(csCode)) { // 供应商角色ID
			orderStateList.add(String.valueOf(OrderStateEnum.C1001.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1002.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1003.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1004.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1005.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1006.getCode()));
			buOrder.setOrderStateList(orderStateList);
			buOrder.setSupplierId(user.getId());
			buOrder.setOrderType("1");
			page = iBuOrderService.findSupplierPage( new Page<BuOrder>(request, response), buOrder);
		}else if(roleId.equals(csCode_001)) {
			orderStateList.add(String.valueOf(OrderStateEnum.C1001.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1002.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1003.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1004.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1005.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1006.getCode()));
			buOrder.setOrderStateList(orderStateList);
			buOrder.setSupplierId(user.getId());
			buOrder.setOrderType("1");
			page = iBuOrderService.findSupplierPage( new Page<BuOrder>(request, response), buOrder);
		} else if (roleId.equals(gdCode)) { // 跟单角色ID
			orderStateList.add(String.valueOf(OrderStateEnum.C1000.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1001.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1002.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1003.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1004.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1005.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1006.getCode()));
			buOrder.setOrderStateList(orderStateList);
			buOrder.setSupplierId(user.getId());
			buOrder.setOrderType("1");
			page = buOrderService.findPage(new Page<BuOrder>(request, response), buOrder);
		} else { // 管理员角色
			orderStateList.add(String.valueOf(OrderStateEnum.C1000.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1001.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1002.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1003.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1004.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1005.getCode()));
			orderStateList.add(String.valueOf(OrderStateEnum.C1006.getCode()));
			buOrder.setOrderStateList(orderStateList);
			buOrder.setOrderType("1");
			page = buOrderService.findPage(new Page<BuOrder>(request, response), buOrder);
		}

		User customerUser = new User();
		Role customerRole = new Role();
		customerRole.setId("6"); // 业务员
		customerUser.setRole(customerRole);
		customerUser.setDelFlag("0");

		User supplierUser = new User();
		Role supplierRole = new Role();
		supplierRole.setId("fc7990c5e8b3476a900adf27eff22124"); // 厂商角色ID
		supplierRole.setDelFlag("0");
		supplierUser.setRole(supplierRole);

		List<User> supplierList = userService.findRef(supplierUser);
		List<User> customerList = userService.findRef(customerUser);

		model.addAttribute("orderState", "110");
		model.addAttribute("roleId", roleId);
		model.addAttribute("page", page);
		model.addAttribute("supplierList", supplierList);
		model.addAttribute("customerList", customerList);
		
		model.addAttribute("httpPriffix", httpPriffix);

		return "modules/bu/buExternalOrderList";
	}

	@RequestMapping(value = "selectSalesList")
	@ResponseBody
	public Map<String, Object> selectSalesList(BuOrder order, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		String customerId = request.getParameter("id");
		BuSalesman salesman = new BuSalesman();
		salesman.setCustomerId(customerId);
		List<BuSalesman> entityList = buSalesManService.findList(salesman);
		Map<String, Object> map = new HashMap<>();
		map.put("salesmanId", entityList);
		return map;

	}

	@RequiresPermissions("bu:buExternalOrder:operate:delivery")
	@RequestMapping(value = "selectById")
	public String selectById(BuOrder buOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
		String id = request.getParameter("id");
		String flag = request.getParameter("flag");
		String write = request.getParameter("write");
		String orderState = request.getParameter("orderState");
		model.addAttribute("id", id);
		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		model.addAttribute("orderState", orderState);
		return "modules/bu/buExternalOrderDelivery";
	}

	@RequiresPermissions("bu:buExternalOrder:operate:update")
	@RequestMapping(value = "updateDelivery")
	public String updateDelivery(HttpServletRequest request, HttpServletResponse response, BuOrder buOrder, Model model,
			RedirectAttributes redirectAttributes) {

		String flag = request.getParameter("flag");
		String write = request.getParameter("write");
		String id = request.getParameter("id");
		User user = UserUtils.getUser();

		if (!beanValidator(model, buOrder)) {
			return form(request, response, buOrder, model);
		}
		if(buOrder.getOrderState().equals("1000")){
			buOrder.setOrderState("1001");
		}else if(buOrder.getOrderState().equals("1001")){
			buOrder.setOrderState("1002");
		}else if(buOrder.getOrderState().equals("1002")){
			buOrder.setOrderState("1003");
		}else if(buOrder.getOrderState().equals("1003")){
			buOrder.setOrderState("1004");
		}else if(buOrder.getOrderState().equals("1004")){
			buOrder.setOrderState("1005");
		}else if(buOrder.getOrderState().equals("1005")){
			buOrder.setOrderState("1006");
		}
		buOrder.setId(id);
		buOrder.setUpdateBy(user);
		buOrder.setUpdateDate(new Date());
		buOrderService.updateDelivery(buOrder);
		
		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:" + Global.getAdminPath() + "/bu/buExternalOrder/?repage";
		
	}
	
	@RequiresPermissions("bu:buExternalOrder:operate:toUpdate")
	@RequestMapping(value = "toUpdate")
	public String toUpdate(HttpServletRequest request, HttpServletResponse response, BuOrder buOrder, Model model) {
		
		String flag = request.getParameter("flag") == null ? "" : request.getParameter("flag");
		String write = request.getParameter("write");

		String customerId = buOrder.getCustomerId();
		String supplierId = buOrder.getSupplierId();
		
		User customerUser = new User();
		customerUser.setId(customerId);
		customerUser.setDelFlag("0");

		User supplierUser = new User();
		supplierUser.setId(supplierId);
		supplierUser.setDelFlag("0");

		List<User> supplierList = userService.findRef(supplierUser);
		List<User> customerList = userService.findRef(customerUser);
		
		BuSalesman salesman = new BuSalesman();
		salesman.setCustomerId(customerId);
		List<BuSalesman> entityList = buSalesManService.findList(salesman);
		
		model.addAttribute("entityList", entityList);
		model.addAttribute("supplierList", supplierList);
		model.addAttribute("customerList", customerList);
		model.addAttribute("buOrder", buOrder);
		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		return "modules/bu/buExternalOrderEdit";

	}

	@RequiresPermissions("bu:buExternalOrder:view")
	@RequestMapping(value = "form")
	public String form(HttpServletRequest request, HttpServletResponse response, BuOrder buOrder, Model model) {

		String flag = request.getParameter("flag") == null ? "" : request.getParameter("flag");
		String write = request.getParameter("write");
		String orderNo = DateUtil.getDate();
		buOrder.setOrderNo(orderNo);
        User user = UserUtils.getUser();
        
		User customerUser = new User();
		Role customerRole = new Role();
		customerRole.setId("6"); // 业务员
		customerUser.setRole(customerRole);
		customerUser.setDelFlag("0");

		User supplierUser = new User();
		Role supplierRole = new Role();
		supplierRole.setId("fc7990c5e8b3476a900adf27eff22124"); // 厂商角色ID
		supplierRole.setDelFlag("0");
		supplierUser.setRole(supplierRole);

		List<User> supplierList = userService.findRefRole(supplierUser);
		List<User> customerList = userService.findRefRole(customerUser);

		model.addAttribute("supplierList", supplierList);
		model.addAttribute("customerList", customerList);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        buOrder.setOrderDate(format.format(new Date()));
        buOrder.setCreateUser(user.getName());
		model.addAttribute("buOrder", buOrder);
		model.addAttribute("write", write);
		model.addAttribute("flag", flag);
		return "modules/bu/buExternalOrderForm";

	}

	/**
	 * 查看
	 * @param request
	 * @param response
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:view")
	@RequestMapping(value = "view")
	public String view(HttpServletRequest request, HttpServletResponse response, BuOrder buOrder, Model model) {

		String flag = request.getParameter("flag");
		String write = request.getParameter("write");
		
		String customerId = buOrder.getCustomerId();
		String supplierId = buOrder.getSupplierId();

		User customerUser = new User();
		customerUser.setId(customerId);
		customerUser.setDelFlag("0");
		
		User supplierUser = new User();
		supplierUser.setId(supplierId);
		supplierUser.setDelFlag("0");

		List<User> supplierList = userService.findRef(supplierUser);
		List<User> customerList = userService.findRef(customerUser);
		
		BuSalesman salesman = new BuSalesman();
		salesman.setCustomerId(customerId);
		List<BuSalesman> entityList = buSalesManService.findList(salesman);
		
		model.addAttribute("supplierList", supplierList);
		model.addAttribute("customerList", customerList);
		model.addAttribute("entityList", entityList);
		model.addAttribute("urlPreffix", httpPriffix);

		model.addAttribute("buOrder", buOrder);
		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		return "modules/bu/buExternalOrderView";

	}

	/**
	 * 新增数据保存
	 * @param request
	 * @param response
	 * @param buOrder
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:edit")
	@RequestMapping(value = "save")
	public String save(HttpServletRequest request, HttpServletResponse response, BuOrder buOrder, Model model,
			RedirectAttributes redirectAttributes) {
		
		if (!beanValidator(model, buOrder)) {
			return form(request, response, buOrder, model);
		}
		String flag = request.getParameter("flag");
		String write = request.getParameter("write");
		User user = UserUtils.getUser();

		Office office = user.getOffice();
		buOrder.setOffice(office);
		String siteId = Site.getCurrentSiteId();
		Site site = new Site();
		site.setId(siteId);
		buOrder.setSite(site);
		if(buOrder.getPicUrl().contains("dev_desk")){
			String[] bb = buOrder.getPicUrl().split("dev_desk");
			System.out.println(bb[0] + "        " + bb[1]);
			buOrder.setPicUrl(bb[1]);
		}
		
		String customerId = buOrder.getCustomerId();
		String supplierId = buOrder.getSupplierId();
		
		User customerUser = new User();
		customerUser.setId(customerId);
		customerUser.setDelFlag("0");

		User supplierUser = new User();
		supplierUser.setId(supplierId);
		supplierUser.setDelFlag("0");

		List<User> supplierList = userService.findRef(supplierUser);
		List<User> customerList = userService.findRef(customerUser);
		
		buOrder.setSupplierName(supplierList.get(0).getName());
		buOrder.setCustomerName(customerList.get(0).getName());
		
		BuSalesman salesMan = buSalesManService.get(buOrder.getSalesmanId());
		
		buOrder.setSalesManName(salesMan.getSalesmanName());
		
		buOrder.setCreateBy(user);
		buOrder.setDelFlag("0");
		buOrder.setOrderState("1000");
		buOrder.setOrderType("1");
		buOrderService.save(buOrder);
		
		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:" + Global.getAdminPath() + "/bu/buExternalOrder/?repage";
		
	}

	protected BuOrderCp constructBuOrderCpEntity(BuOrder buOrder) {

		BuOrderCp orderCp = new BuOrderCp();

		orderCp.setOrderNo(buOrder.getOrderNo());
		orderCp.setProductNo(buOrder.getProductNo());
		orderCp.setOrderDate(buOrder.getOrderDate());
		orderCp.setShipDate(buOrder.getShipDate());
		orderCp.setProductName(buOrder.getProductName());

		orderCp.setCrafts(buOrder.getCrafts());
		orderCp.setElectroplate(buOrder.getElectroplate());
		orderCp.setBack(buOrder.getBack());
		orderCp.setPackages(buOrder.getPackages());
		orderCp.setFitting(buOrder.getFitting());

		orderCp.setPicUrl(buOrder.getPicUrl());
		orderCp.setQuantity(buOrder.getQuantity());
		orderCp.setSalesPrice(buOrder.getSalesPrice());
		orderCp.setSalesModelFee(buOrder.getSalesModelFee());
		orderCp.setProduceFee(buOrder.getProduceFee());

		orderCp.setProduceModelFee(buOrder.getProduceModelFee());
		orderCp.setSalesTotalFee(buOrder.getSalesTotalFee());
		orderCp.setProduceTotalFee(buOrder.getProduceTotalFee());

		return orderCp;

	}

	/**
	 * 订单删除
	 * @param buOrder
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:delete")
	@RequestMapping(value = "delete")
	public String delete(BuOrder buOrder, RedirectAttributes redirectAttributes) {
		buOrderService.delete(buOrder);
		addMessage(redirectAttributes, "删除单表成功");
		return "redirect:" + Global.getAdminPath() + "/bu/buExternalOrder/?repage";
	}

	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:copy")
	@RequestMapping(value = "copy")
	public String copy(HttpServletRequest request, BuOrder buOrder, Model model) {

		String flag = request.getParameter("flag");
		String write = request.getParameter("write");
		
		String customerId = buOrder.getCustomerId();
		String supplierId = buOrder.getSupplierId();
		
		User customerUser = new User();
		customerUser.setId(customerId);
		customerUser.setDelFlag("0");

		User supplierUser = new User();
		supplierUser.setId(supplierId);
		supplierUser.setDelFlag("0");

		List<User> supplierList = userService.findRef(supplierUser);
		List<User> customerList = userService.findRef(customerUser);

		model.addAttribute("supplierList", supplierList);
		model.addAttribute("customerList", customerList);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		buOrder.setOrderDate(format.format(new Date()));
		
		BuSalesman salesman = new BuSalesman();
		salesman.setCustomerId(customerId);
		List<BuSalesman> entityList = buSalesManService.findList(salesman);
		
		model.addAttribute("entityList", entityList);
		
		model.addAttribute("buOrder", buOrder);
		buOrder.setOrderNo(DateUtil.getDate());
		buOrder.setId("");
		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		return "modules/bu/buExternalOrderCopy";
	}

	/**
	 * 查询订单状态
	 * 
	 * @return
	 */
	@RequestMapping(value = "orderStatus")
	@ResponseBody
	public Map<String, Object> getOrderStatus(HttpServletRequest request) {

		Map<String, Object> map = new HashMap<>();

		// 订单状态(0:待审核;1:待生产;2:生产中;3:待发货;4:待收货;5:待收款;6:已完成)
		int externalPendingReview = 0; // 外部待审核订单状态
		int externalToBeProduced = 0; // 外部待审核订单状态
		int externalInProduction = 0; // 外部生产中订单状态
		int externalToBeDelivered = 0; // 外部待发货订单状态
		int externalPendingReceipt = 0; // 外部待收货订单状态
		int externalPendingPayment = 0; // 外部待收款订单状态
		int externalCompleted = 0; // 外部已完成订单状态

		int internalPendingReview = 0; // 内部待审核订单状态
		int internalToBeProduced = 0; // 内部待审核订单状态
		int internalInProduction = 0; // 内部生产中订单状态
		int internalToBeDelivered = 0; // 内部待发货订单状态
		int internalPendingReceipt = 0; // 内部待收货订单状态
		int internalPendingPayment = 0; // 内部待收款订单状态
		int internalCompleted = 0; // 内部已完成订单状态

		boolean trueFlag = true;
		boolean falseFlag = false;

		int internalOrderType = 0;
		int externalOrderType = 1;

		if (trueFlag == true) {

			BuOrder internalPendingReviewOrder = new BuOrder();
			internalPendingReviewOrder.setOrderState("1000");
			internalPendingReviewOrder.setOrderType(String.valueOf(internalOrderType));
			internalPendingReview = buOrderService.selectOrderStatus(internalPendingReviewOrder);
			map.put("internalPendingReview", internalPendingReview);

			BuOrder internalToBeProducedOrder = new BuOrder();
			internalToBeProducedOrder.setOrderState("1001");
			internalToBeProducedOrder.setOrderType(String.valueOf(internalOrderType));
			internalToBeProduced = buOrderService.selectOrderStatus(internalToBeProducedOrder);
			map.put("internalToBeProduced", internalToBeProduced);

			BuOrder internalInProductionOrder = new BuOrder();
			internalInProductionOrder.setOrderState("1002");
			internalInProductionOrder.setOrderType(String.valueOf(internalOrderType));
			internalInProduction = buOrderService.selectOrderStatus(internalInProductionOrder);
			map.put("internalInProduction", internalInProduction);

			BuOrder internalToBeDeliveredOrder = new BuOrder();
			internalToBeDeliveredOrder.setOrderState("1003");
			internalToBeDeliveredOrder.setOrderType(String.valueOf(internalOrderType));
			internalToBeDelivered = buOrderService.selectOrderStatus(internalToBeDeliveredOrder);
			map.put("internalToBeDelivered", internalToBeDelivered);

			BuOrder internalPendingReceiptOrder = new BuOrder();
			internalPendingReceiptOrder.setOrderState("1004");
			internalPendingReceiptOrder.setOrderType(String.valueOf(internalOrderType));
			internalPendingReceipt = buOrderService.selectOrderStatus(internalPendingReceiptOrder);
			map.put("internalPendingReceipt", internalPendingReceipt);

			BuOrder internalPendingPaymentOrder = new BuOrder();
			internalPendingPaymentOrder.setOrderState("1005");
			internalPendingPaymentOrder.setOrderType(String.valueOf(internalOrderType));
			internalPendingPayment = buOrderService.selectOrderStatus(internalPendingPaymentOrder);
			map.put("internalPendingPayment", internalPendingPayment);

			BuOrder internalCompletedOrder = new BuOrder();
			internalCompletedOrder.setOrderState("1006");
			internalCompletedOrder.setOrderType(String.valueOf(internalOrderType));
			internalCompleted = buOrderService.selectOrderStatus(internalCompletedOrder);
			map.put("internalCompleted", internalCompleted);

		}
		if (falseFlag == false) {

			BuOrder externalPendingReviewOrder = new BuOrder();
			externalPendingReviewOrder.setOrderState("1000");
			externalPendingReviewOrder.setOrderType(String.valueOf(externalOrderType));
			externalPendingReview = buOrderService.selectOrderStatus(externalPendingReviewOrder);
			map.put("externalPendingReview", externalPendingReview);

			BuOrder externalToBeProducedOrder = new BuOrder();
			externalToBeProducedOrder.setOrderState("1001");
			externalToBeProducedOrder.setOrderType(String.valueOf(externalOrderType));
			externalToBeProduced = buOrderService.selectOrderStatus(externalToBeProducedOrder);
			map.put("externalToBeProduced", externalToBeProduced);

			BuOrder externalInProductionOrder = new BuOrder();
			externalInProductionOrder.setOrderState("1002");
			externalInProductionOrder.setOrderType(String.valueOf(externalOrderType));
			externalInProduction = buOrderService.selectOrderStatus(externalInProductionOrder);
			map.put("externalInProduction", externalInProduction);

			BuOrder externalToBeDeliveredOrder = new BuOrder();
			externalToBeDeliveredOrder.setOrderState("1003");
			externalToBeDeliveredOrder.setOrderType(String.valueOf(externalOrderType));
			externalToBeDelivered = buOrderService.selectOrderStatus(externalToBeDeliveredOrder);
			map.put("externalToBeDelivered", externalToBeDelivered);

			BuOrder externalPendingReceiptOrder = new BuOrder();
			externalPendingReceiptOrder.setOrderState("1004");
			externalPendingReceiptOrder.setOrderType(String.valueOf(externalOrderType));
			externalPendingReceipt = buOrderService.selectOrderStatus(externalPendingReceiptOrder);
			map.put("externalPendingReceipt", externalPendingReceipt);

			BuOrder externalPendingPaymentOrder = new BuOrder();
			externalPendingPaymentOrder.setOrderState("1005");
			externalPendingPaymentOrder.setOrderType(String.valueOf(externalOrderType));
			externalPendingPayment = buOrderService.selectOrderStatus(externalPendingPaymentOrder);
			map.put("externalPendingPayment", externalPendingPayment);

			BuOrder externalCompletedOrder = new BuOrder();
			externalCompletedOrder.setOrderState("1006");
			externalCompletedOrder.setOrderType(String.valueOf(externalOrderType));
			externalCompleted = buOrderService.selectOrderStatus(externalCompletedOrder);
			map.put("externalCompleted", externalCompleted);

		}
		return map;
	}
	
	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:eos0")
	@RequestMapping(value = "eos0")
	public void eos0(HttpServletRequest request, BuOrder buOrder, Model model) {
		
	}
	
	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:eos1")
	@RequestMapping(value = "eos1")
	public void eos1(HttpServletRequest request, BuOrder buOrder, Model model) {
		
	}
	
	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:eos2")
	@RequestMapping(value = "eos2")
	public void eos2(HttpServletRequest request, BuOrder buOrder, Model model) {
		
	}
	
	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:eos3")
	@RequestMapping(value = "eos3")
	public void eos3(HttpServletRequest request, BuOrder buOrder, Model model) {
		
	}
	
	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:eos4")
	@RequestMapping(value = "eos4")
	public void eos4(HttpServletRequest request, BuOrder buOrder, Model model) {
		
	}
	
	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:eos5")
	@RequestMapping(value = "eos5")
	public void eos5(HttpServletRequest request, BuOrder buOrder, Model model) {
		
	}
	
	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:eos8")
	@RequestMapping(value = "eos8")
	public void eos8(HttpServletRequest request, BuOrder buOrder, Model model) {
		
	}
	
	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:add")
	@RequestMapping(value = "addExternalOrder")
	public String addExternalOrder(HttpServletRequest request, BuOrder buOrder, Model model) {
		
		String flag = request.getParameter("flag");
		String write = request.getParameter("write");
		String orderNo = DateUtil.getDate();
		buOrder.setOrderNo(orderNo);
		User user = UserUtils.getUser();
		buOrder.setCreateBy(user);

		User customerUser = new User();
		Role customerRole = new Role();
		customerRole.setId("6"); // 业务员
		customerUser.setRole(customerRole);
		customerUser.setDelFlag("0");

		User supplierUser = new User();
		Role supplierRole = new Role();
		supplierRole.setId("fc7990c5e8b3476a900adf27eff22124"); // 厂商角色ID
		supplierRole.setDelFlag("0");
		supplierUser.setRole(supplierRole);

		List<User> supplierList = userService.findRef(supplierUser);
		List<User> customerList = userService.findRef(customerUser);

		model.addAttribute("supplierList", supplierList);
		model.addAttribute("customerList", customerList);

		model.addAttribute("buOrder", buOrder);
		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		return "modules/bu/buExternalOrderForm";

	}
	
	/**
	 * 下载生产单
	 * @param buOrder
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:export")
    @RequestMapping(value = "exportProductFile", method=RequestMethod.GET)
    public String exportProductFile(BuOrder buOrder, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            ExcelTest.reportOrderMergeXls(buOrder, "xx", "yy",response,httpPriffix,winUserfilesBasedir,linuxUserfilesBasedir);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出订单失败！失败信息："+e.getMessage());
		}
		return "redirect:" + adminPath + "/bu/buExternalOrder/list?repage";
    }
	
	/**
	 * 导出订单数据
	 * @param buOrder
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:view")
    @RequestMapping(value = "downloadProductFile", method=RequestMethod.POST)
    public String downloadProductFile(BuOrder buOrder, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "订单数据_"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            
            // 订单状态(0:待审核;1:待生产;2:生产中;3:待发货;4:待收货;5:待收款;6:已完成)
    		User user = UserUtils.getUser();
    		List<String> roleList = user.getRoleIdList();
    		Page<BuOrder> page = null;
    		String roleId = String.valueOf(roleList.get(0));
    		List<String> orderStateList = new ArrayList<>();
    		String yeCode = "6";
    		String csCode = "7952384aba3e401aa0b4cc713d5b71b1";
    		String gdCode = "1a894ce8e6014efb9d21fb922bf8ceb2";
    		if (roleId.equals(yeCode)) { // 业务员角色ID
    			orderStateList.add(String.valueOf(OrderStateEnum.C1000.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1001.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1002.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1003.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1004.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1005.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1006.getCode()));
    			buOrder.setCustomerId(user.getId());
    			buOrder.setOrderStateList(orderStateList);
    			buOrder.setOrderType("1");
    			page = iBuOrderService.findCustomerPage(new Page<BuOrder>(request,response,-1), buOrder);
    		} else if (roleId.equals(csCode)) { // 供应商角色ID
    			orderStateList.add(String.valueOf(OrderStateEnum.C1000.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1001.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1002.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1003.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1004.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1005.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1006.getCode()));
    			buOrder.setOrderStateList(orderStateList);
    			buOrder.setSupplierId(user.getId());
    			buOrder.setOrderType("1");
    			page = iBuOrderService.findSupplierPage(new Page<BuOrder>(request,response,-1), buOrder);
    		} else if (roleId.equals(gdCode)) { // 跟单角色ID
    			orderStateList.add(String.valueOf(OrderStateEnum.C1000.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1001.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1002.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1003.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1004.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1005.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1006.getCode()));
    			buOrder.setOrderStateList(orderStateList);
    			buOrder.setSupplierId(user.getId());
    			buOrder.setOrderType("1");
    			page = buOrderService.findPage(new Page<BuOrder>(request,response,-1), buOrder);
    		} else { // 管理员角色
    			orderStateList.add(String.valueOf(OrderStateEnum.C1000.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1001.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1002.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1003.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1004.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1005.getCode()));
    			orderStateList.add(String.valueOf(OrderStateEnum.C1006.getCode()));
    			buOrder.setOrderStateList(orderStateList);
    			buOrder.setOrderType("1");
    			page = buOrderService.findPage(new Page<BuOrder>(request,response,-1), buOrder);
    		}
    		new ExportExcel("数据", BuOrder.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			e.printStackTrace();
			addMessage(redirectAttributes, "导出订单数据失败！失败信息："+e.getMessage());
		}
		return "redirect:" + adminPath + "/bu/buExternalOrder/list?repage";
    }
	
	/**
	 * 批量收款
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:batchReceipt")
	@RequestMapping(value = "batchReceipt")
	public String batchReceipt(HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes){
		String ids = request.getParameter("ids");
		String[] idsArr = ids.split(",");
		List<String> idList = new ArrayList<>();
		for(int i = 0;i<idsArr.length;i++){
			idList.add(idsArr[i]);
		}
		BuOrder buOrder = new BuOrder();
		buOrder.setOrderState("1006");
		User user = UserUtils.getUser();
		buOrder.setUpdateBy(user);
		buOrder.setUpdateDate(new Date());
		buOrder.setIdList(idList);
		buOrderService.updateBatchReceipt(buOrder);
		return "redirect:" + adminPath + "/bu/buExternalOrder/list?repage";
	}
	
	
	/**
	 * 销售总价
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:salesTotalFee")
	@RequestMapping(value = "salesTotalFee")
	public void salesTotalFee(HttpServletRequest request, BuOrder buOrder, Model model) {
		
	}
	
	/**
	 * 销售单价
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:salesPrice")
	@RequestMapping(value = "salesPrice")
	public void salesPrice(HttpServletRequest request, BuOrder buOrder, Model model) {
		
	}
	
	/**
	 * 销售模费
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:salesModelFee")
	@RequestMapping(value = "salesModelFee")
	public void salesModelFee(HttpServletRequest request, BuOrder buOrder, Model model) {
		
	}
	
	/**
	 * 利润
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:profit")
	@RequestMapping(value = "profit")
	public void profit(HttpServletRequest request, BuOrder buOrder, Model model) {
		
	}
	
}